﻿Imports System.Activities
Imports System.Management
Imports System.IO
Imports System.Activities.Presentation.Metadata
Imports System.ComponentModel
Imports System.Drawing
Imports System.Activities.Presentation

Public Class WMISYSGetInstalledSoftwares
    Inherits WMIExecuteActivityBase

    Private Const HKLM As UInt32 = &H80000002L
    Private Const BASE_KEY = "Software\Microsoft\Windows\CurrentVersion\Uninstall\"


    Public Sub New()
        Me.DisplayName = My.Resources.GetInstalledSoftwaresDisplayName
    End Sub

    Protected Overrides Function BeginExecute(ByVal context As AsyncCodeActivityContext, ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult
        MyBase._target = " "
        MyBase._where = ""
        MyBase._service = "Default"

        Return MyBase.BeginExecute(context, callback, state)
    End Function

    Protected Overrides Function ExecuteWMI(ByVal target As String, ByVal whereCondition As String, ByVal wmScope As ManagementScope) As Generic.List(Of Hashtable)
        Dim result As New List(Of Hashtable)
        Try
            wmScope = CreateScope(wmScope)

            Dim classInstance As New ManagementClass(wmScope, New ManagementPath("StdRegProv"), Nothing)

            Dim methodArgs = New Object() {HKLM, BASE_KEY, Nothing}
            Dim res = classInstance.InvokeMethod("EnumKey", methodArgs)
            If methodArgs(2) Is Nothing Then Exit Try

            Dim subkeys = CType(methodArgs(2), String())
            If subkeys Is Nothing Then Exit Try

            Dim inParams As ManagementBaseObject = classInstance.GetMethodParameters("GetStringValue")
            inParams("hDefKey") = HKLM

            For Each child In subkeys
                inParams("sSubKeyName") = BASE_KEY + child
                inParams("sValueName") = "DisplayName"

                Dim outParams As ManagementBaseObject = classInstance.InvokeMethod("GetStringValue", inParams, Nothing)
                If outParams IsNot Nothing Then
                    Dim resRec As New Hashtable
                    For Each resCol In outParams.Properties
                        If resCol.Value Is Nothing Then
                            resRec.Add(resCol.Name, Nothing)
                        Else
                            resRec.Add(resCol.Name, resCol.Value.ToString)
                        End If
                    Next
                    result.Add(resRec)
                End If
            Next

        Catch ex As Exception
            result.Clear()
        End Try
        Return result
    End Function

    Protected Overrides Sub CacheMetadata(metadata As CodeActivityMetadata)
        MyBase.CacheMetadata(metadata)

        Dim build As New AttributeTableBuilder
        'WMISYSGetInstalledSoftwares Activity
        build.AddCustomAttributes(Me.GetType,
                                      New DesignerAttribute(GetType(WMIDefaultActivityDesigner), GetType(ActivityDesigner)),
                                      New ToolboxBitmapAttribute(Me.GetType, "WmiDefault.png"))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub

End Class
